package com.global.variable.common.web.utils;

import com.basic.management.facade.services.EmployeeService;
import com.basic.management.model.vo.auth.EmployeeVO;
import com.basic.management.model.vo.auth.Response;
import com.global.variable.common.web.constants.Constants;
import com.global.variable.common.web.redis.RedisService;

import javax.servlet.http.HttpServletRequest;

/**
 * company 重庆庆云石油工程技术有限责任公司
 * FileName UserUtils
 * Package com.cloud.intelligence.management.platform.intelligenceplatform.util
 * Description 用户工具类
 * author Administrator
 * create 2018-10-19 下午 3:44
 * version V1.0
 */
public final class UserUtils {


    public static EmployeeVO getCurrentEmployee(HttpServletRequest request){
        String token = WebUtils.getCookieValue(request, Constants.TOKEN_NAME);
        RedisService redisService = SpringContextHolder.getBean(RedisService.class);
        EmployeeVO employeeVO =(EmployeeVO)redisService.get(token);
        if(null == employeeVO){
            employeeVO = getEmployeeForApp(request);
        }
        return employeeVO;
    }

    private static EmployeeVO getEmployeeForApp(HttpServletRequest request){
        String empId = (String) request.getAttribute("userId");
        EmployeeService employeeService = SpringContextHolder.getBean(EmployeeService.class);
        Response response = employeeService.getById(empId);
        if(response.isOk()){
            return (EmployeeVO)response.getData();
        }
        return null;
    }

}
